﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.ComponentModel;
//using System.Timers;
using System.Web;
using System.IO;
using System.Net;
using System.Data;
//using System.Windows.Forms;
using HtmlAgilityPack;

namespace Extractor.LibCode
{
    public struct DefineBabe
    {
        public string MODELID;
        public string NAME;
        public string DOB;
        public string ALIASES;
        public string COUNTRY;
        public string STATE;
        public string TOWN;
        public string ETNIC;
        public string EYE;
        public string HAIR;
        public string WEIGHT;
        public string HEIGHT;
        public string CUP;
        public string MEASURE;
        public string URI;
        public string OFFICIALSITE;
        public string THUMBNAIL;
        public string Umur;
    }
    public struct ModelGallery
    {
        public string GALLERYID;
        public string MODELID;
        public string GALLGROUP;
        public string GALTITLE;
        public string PICCOUNT;
        public string DATE;
        public string URI;
        public string THUMBNAIL;
        public string THUMBDOWNLOADED;


    }



    public class cDefineBb : cDBService
    {
        private int curDlwd = 0;
        private int ibusy = 0;
        private StringBuilder sb = new StringBuilder();
        static BackgroundWorker _bw;
        private static int cTh = 0;
        
        #region INDEX MODEL
        public string[] IndexModels = new string[] {"http://www.definebabe.com/models/a/",
                                                    "http://www.definebabe.com/models/b/",
                                                    "http://www.definebabe.com/models/c/",
                                                    "http://www.definebabe.com/models/d/",
                                                    "http://www.definebabe.com/models/e/",
                                                    "http://www.definebabe.com/models/f/",
                                                    "http://www.definebabe.com/models/g/",
                                                    "http://www.definebabe.com/models/h/",
                                                    "http://www.definebabe.com/models/i/",
                                                    "http://www.definebabe.com/models/j/",
                                                    "http://www.definebabe.com/models/k/",
                                                    "http://www.definebabe.com/models/l/",
                                                    "http://www.definebabe.com/models/m/",
                                                    "http://www.definebabe.com/models/n/",
                                                    "http://www.definebabe.com/models/o/",
                                                    "http://www.definebabe.com/models/p/",
                                                    "http://www.definebabe.com/models/q/",
                                                    "http://www.definebabe.com/models/r/",
                                                    "http://www.definebabe.com/models/s/",
                                                    "http://www.definebabe.com/models/t/",
                                                    "http://www.definebabe.com/models/u/",
                                                    "http://www.definebabe.com/models/v/",
                                                    "http://www.definebabe.com/models/w/",
                                                    "http://www.definebabe.com/models/x/",
                                                    "http://www.definebabe.com/models/y/",
                                                    "http://www.definebabe.com/models/z/"
                                                    };
        #endregion

        public cDefineBb()
        {
            


        }


        public DataTable getimgbkp(string sql)
        {
            return Dbserver.ExecuteQuery(sql, "");

        }

        void downloadimg(object obj)
        {
            //MessageBox.Show((string)myuri);
            //sb.AppendLine((string)myuri);
            //ibusy--;
            WebClient wc = new WebClient();
            wc.DownloadFileCompleted+=new AsyncCompletedEventHandler(wc_DownloadFileCompleted);
            string[] ss = (string[])obj;
            string gallid = ss[1].Replace("http://www.abc.com/", "");
            //string modelid = ss[2];
            
            
            wc.BaseAddress = ss[1];
            int im = int.Parse(gallid);
            string spath = Properties.Settings.Default.dfpath + "\\" + getfolder(im,2000);
            if (!Directory.Exists(spath))
            {
                Directory.CreateDirectory(spath);

            }
            
            wc.DownloadFileAsync(new Uri(ss[0]), spath+"\\"+gallid+".jpg");
            
            
        }
     
        public string getfolder(int i,int igroup)
        {
            int c = 0;
            if (i % igroup == 0)
            {
                c = (i / igroup) * igroup;
            }
            else
            {
                c = ((i / igroup) + 1) * igroup;
               
            }
            return c.ToString();

        

        }
        

        void wc_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            string modelid = ((WebClient)sender).BaseAddress.Replace("http://www.abc.com/", ""); 
            if (e.Error != null)
            {
                cCommon.kotakpesan(modelid + "-" + e.Error);
            }
            else
            {
                lock (this)
                {
                    //string sqlu = "update tbmodel set downloaded='Y' where modelid='" + modelid + "'";
                    string sqlu = "update tbmodelgallerydet set thumbdownloaded='Y' where galleryid='" + modelid + "'";
                    if (Dbserver.openConn())
                    {
                        Dbserver.ExecuteNonQuery(sqlu);
                        Dbserver.CommitAndCloseConn();
                    }
                }

            }

            ibusy--;
            //Thread.Sleep(10);
        }
        static readonly object locker = new object();

        void startdownloadGallerydet(object obj)
        {
            string mdl = (string)obj;
            string sql = "select top 3000 * from tbmodelgallerydet where (thumbdownloaded<>'Y' or thumbdownloaded is null) ";
            if (mdl.Trim() != string.Empty)
                sql += " and modelid='"+mdl+"'";
            DataTable dt = Dbserver.ExecuteQuery(sql, "tbmodelgallerydet");
            int i = dt.Rows.Count - 1;

            while (curDlwd <= i)
            {
                Thread.Sleep(100);
                if (ibusy < cTh)
                {

                    lock (locker)
                    {
                        ibusy++;

                        Thread t = new Thread(downloadimg);
                        //string stemp = string.Join("|", dt.Rows[curDlwd]["thumbnail"].ToString(), dt.Rows[curDlwd]["modelid"].ToString());
                        string[] stemp = new string[] { dt.Rows[curDlwd]["thumbnail"].ToString().Replace("/thumb1/","/thumb2/"), "http://www.abc.com/" + dt.Rows[curDlwd]["galleryid"].ToString() };
                        string ss = curDlwd.ToString();
                        t.Start(stemp);
                        curDlwd++;
                    }

                    //dl();

                }
                // Thread.Sleep(100);

            }

            cCommon.kotakpesan("Download Complete");
        }

        void startdownload()
        {
            string sql = "select top 2500 * from tbmodel where downloaded<>'Y' or downloaded is null";
            DataTable dt = Dbserver.ExecuteQuery(sql, "tbmodel");
            int i = dt.Rows.Count - 1;

            while (curDlwd <= i)
            {
                Thread.Sleep(100);
                if (ibusy < cTh)
                {
                   
                    lock (locker)
                    {
                         ibusy++;
                    
                    Thread t = new Thread(downloadimg);
                    //string stemp = string.Join("|", dt.Rows[curDlwd]["thumbnail"].ToString(), dt.Rows[curDlwd]["modelid"].ToString());
                    string[] stemp = new string[] { dt.Rows[curDlwd]["thumbnail"].ToString(), "http://www.abc.com/"+dt.Rows[curDlwd]["modelid"].ToString() };
                        string ss=curDlwd.ToString();
                    t.Start(stemp);
                    curDlwd++;
                   }

                    //dl();

                }
               // Thread.Sleep(100);

            }

            cCommon.kotakpesan("Download Complete");
        }

        public void downloadmodelgallerydet(string modelid,int ct)
        {
            curDlwd = 0;
            ibusy = 0;
            cTh = ct;
            new Thread(startdownloadGallerydet).Start(modelid);


        }
        public void downloadModel(int ct)
        { 
        //    string sql="select top 10 * from tbmodel where downloaded<>'Y' or downloaded is null";
        //    DataTable dt= Dbserver.ExecuteQuery(sql,"tbmodel");
        //    int i = dt.Rows.Count - 1;

        //    List<WebClient> wc = new List<WebClient>();

            curDlwd = 0;
            ibusy = 0;
            cTh = ct;
            new Thread(startdownload).Start();
            //Dictionary<int,WebClient> dwc = new Dictionary<int,WebClient>();

            //WebClient wc = new WebClient();
            //wc.IsBusy;
           //for(int j=0;j<2;j++)
           //{
           //    WebClient www= new WebClient();
           //    //www.
           //    wc.Add(www);
           // //   wc[j].DownloadFileCompleted+=new global::System.ComponentModel.AsyncCompletedEventHandler(cDefineBb_DownloadFileCompleted);
           //    //Completed+=new System.ComponentModel.AsyncCompletedEventHandler(cDefineBb_DownloadFileCompleted);               
           //    //wc[j].d
           //}

           
            //while (curDlwd < i)
            //{
                //if (ibusy < 2)
                //{
                //    ibusy++;

                //    dl(dt.Rows[curDlwd]["thumbnail"].ToString());
                //}
                //Thread.Sleep(100);     
               
           // }


            
           // tm=new Timer(300);

        
        }

      


        void dl(string s)
        {
            WebClient wc = new WebClient();
            
                    curDlwd++;
                    wc.DownloadFileCompleted += new System.ComponentModel.AsyncCompletedEventHandler(wc_DownloadFileCompleted);
                    wc.DownloadFileAsync(new Uri(s), curDlwd.ToString() + ".jpg");
                    wc.BaseAddress = s;// dt.Rows[curDlwd]["thumbnail"].ToString();
            

            
            

        }

       
      

void  cDefineBb_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
            ibusy--;
}

       

        
        public void getALLModelGalleries()
        {
           // getModelGalleries("http://http://www.definebabe.com/models/lfsg/jayden-jaymes/galleries/", "003750", "1", true);
            //return;
            DataTable dt = new DataTable();
            dt = Dbserver.ExecuteQuery("select top 5000 * from tbmodelgallery where  needupdate<>'N' or needupdate is null order by modelid", "tbmodelgallery");
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                    getModelGalleries(dr["GALLERYINDEX"].ToString(), dr["modelid"].ToString(), dr["GALLGROUP"].ToString(), true);
            }
        }

        public string getHTMLModelGallery(string modelid,bool isLarge)
        {
            string sql = string.Format("select * from tbmodelgallerydet where modelid='{0}'", modelid);
            DataTable dt = new DataTable();
            dt = Dbserver.ExecuteQuery(sql, "tbmodelgallerydet");
            if (dt.Rows.Count == 0)
                return "";

            StringBuilder sb = new StringBuilder();
            sb.AppendLine("<html><head><body>");
            string stemp = "<div style=\"position:relative; float:left;\"><img alt=\"\" src=\"[IMG]\"></div>";

            foreach (DataRow dr in dt.Rows)
            {
                if (isLarge)
                {
                    sb.AppendLine(stemp.Replace("[IMG]", dr["Thumbnail"].ToString().Replace("/thumb1/","/thumb2/")));
                }
                else
                {
                    sb.AppendLine(stemp.Replace("[IMG]", dr["Thumbnail"].ToString()));
                }
                
            }



            sb.AppendLine("</body></head></html>");
            return sb.ToString();
        }

        public DataTable galleryInquiry(ModelGallery mg)
        {
            string sql = "select  * from tbmodelgallerydet where 1=1 ";
            if (mg.GALLERYID != null)
                sql += string.Format("and galleryid='{0}' ", mg.GALLERYID);
            if (mg.MODELID != null)
                sql += string.Format("and modelid='{0}' ", mg.MODELID);
            if (mg.GALTITLE != null)
                sql += string.Format("and GALTITLE like '%{0}%' ", mg.GALTITLE);

            DataTable dt = new DataTable();
            dt = Dbserver.ExecuteQuery(sql, "tbmodel");
            return dt;

        }
        public DataTable modelinquiry(DefineBabe df, int pg)
        {
            string sql = "select * from (select ROW_NUMBER() OVER(ORDER BY modelid ) AS nomor ,* from tbmodel where 1=1 ";
            int cp = 10;
            if (df.Umur.Trim() != "")
                sql += "and umur='" + df.Umur + "' ";
            if (df.COUNTRY.Trim() != "")
                sql += "and country='" + df.COUNTRY + "' ";
            if (df.ETNIC.Trim() != "")
                sql += "and etnic='" + df.ETNIC + "' ";
            if (df.CUP.Trim() != string.Empty)
                sql += "and cup='" + df.CUP + "' ";

            if (df.MODELID != null)
                sql += "and modelid=" + df.MODELID + " ";
            if (df.NAME != null)
                sql += "and name like '%" + df.NAME + "%' ";

           


            sql += string.Format(") a where nomor between {0} and {1}", (cp * (pg - 1)) + 1, cp * pg);

            DataTable dt = new DataTable();
            dt = Dbserver.ExecuteQuery(sql, "tbmodel");
            return dt;


        }
        public string getHTMLImageModel(DefineBabe df, int pg)
        {

            DataTable dt = new DataTable();
            dt = modelinquiry(df, pg);
            if (dt.Rows.Count == 0)
                return "REcords Not Found";
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("<html><head><body>");
            string stemp = "<div style=\"position:relative; float:left;\"><img alt=\"\" src=\"[IMG]\" title=\"[TITLE]\"></div>";
            foreach (DataRow dr in dt.Rows)
            {
                
               // sb.AppendLine(stemp.Replace("[IMG]", dr["Thumbnail"].ToString()));
                string ss = stemp.Replace("[IMG]", "file:///" + Properties.Settings.Default.dfpath + "\\" + getfolder(int.Parse(dr["modelid"].ToString()),1000) + "\\" + dr["modelid"].ToString() + ".jpg");
                ss = ss.Replace("[TITLE]", dr["modelid"].ToString() + " " + dr["name"].ToString());
                sb.AppendLine(ss);


                //sb.AppendLine(stemp.Replace("[TITLE]", dr["modelid"].ToString()+" "+ dr["name"].ToString()));
                

                
            }
            sb.AppendLine("</body></head></html>");
            return sb.ToString();


        }
        public void getModelGalleries(string suri, string smodelid, string gallgroup, bool bappend)
        {
            StreamWriter sw = new System.IO.StreamWriter(Properties.Settings.Default.pathdata + "\\ModelGaleries.txt", bappend);

            Uri myUri = new Uri(suri);

            WebClient client = new WebClient();
            string pagehtml = client.DownloadString(myUri);
            //string pagehtml = cCommon.strimLine(System.IO.File.ReadAllLines(Properties.Settings.Default.pathdata + "\\note.htm"));
            HtmlDocument doc = new HtmlDocument();

            doc.Load(new StringReader(pagehtml));
            DataTable dt = new DataTable();
            //dt = Dbserver.ExecuteQuery("select top 1 * from tbmodel where uri='" + suri + "'", "tbmodel");
            //  string modelid = "";
            foreach (HtmlNode hn in doc.DocumentNode.SelectNodes("//div[@class='centr20']"))
            {
                foreach (HtmlNode hn2 in hn.SelectNodes("div"))
                {
                    if (hn2.Attributes["class"] != null)
                    {
                        if (hn2.Attributes["class"].Value == "lblock")
                        {
                            if(hn2.SelectNodes("ul/li")!=null)
                            foreach (HtmlNode hnn in hn2.SelectNodes("ul/li"))
                            {
                                ModelGallery mg = new ModelGallery();
                                mg.MODELID = smodelid;
                                mg.GALLGROUP = gallgroup;
                                DataTable dt2 = new DataTable();


                                HtmlNode cn = hnn.SelectSingleNode("a");
                                if (cn != null)
                                {
                                    mg.GALTITLE = cn.Attributes["title"].Value;
                                    mg.URI = "http://www.definebabe.com" + cn.Attributes["href"].Value;
                                    if (mg.URI == "http://www.definebabe.com/gallery/dy0/tatiana-kush/")
                                    {
                                    }
                                }

                                dt2 = Dbserver.ExecuteQuery(string.Format("select * from tbmodelgallerydet where modelid='{0}' and GALLGROUP='{1}' and uri='{2}'", smodelid, gallgroup, mg.URI), "tbmodelgallerydet");
                                DataRow dr;
                                bool isnew = false;

                               
                                
                                
                                if (dt2.Rows.Count == 0)
                                {
                                    mg.GALLERYID = new cDBHelper().genIDCommit("GALLERYID");
                                    dr = dt2.NewRow();                                    
                                    isnew = true;
                                }
                                else
                                {
                                    dr = dt2.Rows[0];
                                    mg.GALLERYID = dt2.Rows[0]["galleryid"].ToString();
                                }

                                
                                //cn.Remove();
                                HtmlNode cn2 = hnn.SelectSingleNode("a/img");
                                if (cn2 != null)
                                {
                                    mg.THUMBNAIL = cn2.Attributes["src"].Value;

                                }

                                cn.Remove();
                                string[] stemp = hnn.InnerText.Trim().Replace(" pics / ", "|").Split('|');
                                mg.PICCOUNT = stemp[0];
                                if(stemp.Length==2)
                                mg.DATE = stemp[1].Replace(".", "-");
                                if(!mg.PICCOUNT.Contains("."))
                                dr["PICCOUNT"] = mg.PICCOUNT;
                                if(mg.DATE!=null)
                                dr["DATE"] = mg.DATE;
                                dr["THUMBNAIL"] = mg.THUMBNAIL;
                                dr["GALLERYID"] = mg.GALLERYID;
                                dr["MODELID"] = smodelid;
                                dr["GALLGROUP"] = gallgroup;
                                dr["GALTITLE"] = mg.GALTITLE;
                                dr["URI"] = mg.URI;


                                if (isnew)
                                    dt2.Rows.Add(dr);
                                if (Dbserver.openConn())
                                {
                                    string sout = "";
                                    Dbserver.SqlUpdate(dt2, dt2.TableName, out sout);
                                    Dbserver.CommitAndCloseConn();
                                }

                            }
                            setNeedUpdate(smodelid, gallgroup, "N");
                            sw.Close();
                            break;
                        }
                    }
                }
            }
        }

        private void setNeedUpdate(string modelid, string gallgroup,string stat)
        {
            string sql = string.Format("select * from tbmodelgallery where modelid='{0}' and gallgroup='{1}'",  modelid, gallgroup);
            DataTable dt = Dbserver.ExecuteQuery(sql, "tbmodelgallery");
            if(dt.Rows.Count>0)
                if (Dbserver.openConn())
                {
                    string s = "";
                    dt.Rows[0]["NEEDUPDATE"] = stat;
                    Dbserver.SqlUpdate(dt, "tbmodelgallery",out s);
                    Dbserver.CommitAndCloseConn();


                }

        }
        public void getAllModelDetail()
        {
            DataTable dt = new DataTable();
            dt = Dbserver.ExecuteQuery("select modelid,uri from tbmodel where needupdate<>'N' or needupdate is null order by modelid", "tbmodel");
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                    getModelDetail(dr["uri"].ToString(), true);
            }
        }
        public void getModelDetail(string suri, bool bappend)
        {
            StreamWriter sw = new System.IO.StreamWriter(Properties.Settings.Default.pathdata + "\\ModelGaleries.txt", bappend);

            Uri myUri = new Uri(suri);

            WebClient client = new WebClient();
            string pagehtml = client.DownloadString(myUri);
            //string pagehtml = cCommon.strimLine(System.IO.File.ReadAllLines(Properties.Settings.Default.pathdata + "\\note.htm"));
            HtmlDocument doc = new HtmlDocument();

            doc.Load(new StringReader(pagehtml));
            DataTable dt = new DataTable();
            dt = Dbserver.ExecuteQuery("select top 1 * from tbmodel where uri='" + suri + "'", "tbmodel");
            string modelid = "";
            if (dt.Rows.Count > 0)
            {

                #region Class Info / Get Alias Town and State
                DataRow dr = dt.Rows[0];
                modelid = dr["MODELID"].ToString();
                foreach (HtmlNode hn in doc.DocumentNode.SelectNodes("//div[@class='infoblock']"))
                {

                    HtmlNode hnifo = hn.SelectSingleNode("div[@class='info']");
                    string[] sline = hnifo.InnerText.Split('\n'); //"\n", StringSplitOptions.None);
                    foreach (string span in sline)
                    {
                        if (span.Trim().Contains("Aliases: "))
                            dr["ALIASES"] = span.Replace("Aliases: ", "").Trim();
                        if (span.Trim().Contains("State: "))
                            dr["STATE"] = span.Replace("State: ", "").Trim();
                        if (span.Trim().Contains("Town: "))
                            dr["TOWN"] = span.Replace("Town: ", "").Trim();
                    }
                    HtmlNode hnoffsite = hn.SelectSingleNode("a");
                    if (hnoffsite != null)
                        dr["OFFICIALSITE"] = hnoffsite.Attributes["href"].Value;

                    if (Dbserver.openConn())
                    {
                        string s = "";
                        Dbserver.SqlUpdate(dt, dt.TableName, out s);
                        Dbserver.CommitAndCloseConn();

                    }
                }
                #endregion

                client = new WebClient();

                //pagehtml = cCommon.strimLine(System.IO.File.ReadAllLines(Properties.Settings.Default.pathdata + "\\note2.htm"));
                pagehtml = client.DownloadString(myUri + "galleries/");

                doc.Load(new StringReader(pagehtml));

                dt = new cDBHelper().getTableStructure("TBMODELGALLERY");

                foreach (HtmlNode hn in doc.DocumentNode.SelectNodes("//div[@class='pages']"))
                {
                    dr = dt.NewRow();
                    dr["modelid"] = modelid;
                    dr["galleryindex"] = myUri + "galleries/";
                    dt.Rows.Add(dr);
                    sw.WriteLine(dr["galleryindex"]);
                    if (hn.SelectNodes("a") != null)
                        foreach (HtmlNode hn3 in hn.SelectNodes("a"))
                        {
                            if (hn3 != null)
                            {
                                if (!hn3.InnerText.Contains("Next"))
                                {

                                    dr = dt.NewRow();
                                    dr["modelid"] = modelid;
                                    dr["galleryindex"] = "http://www.definebabe.com" + hn3.Attributes["href"].Value;
                                    dt.Rows.Add(dr);
                                    sw.WriteLine(dr["galleryindex"]);
                                }


                            }
                        }
                    if (Dbserver.openConn())
                    {
                        string s = "";
                        Dbserver.SqlUpdate(dt, dt.TableName, out s);
                        Dbserver.CommitAndCloseConn();
                    }

                }
            }
            sw.Close();
        }


        public void getModelPageAll()
        {
            string pagehtml = cCommon.strimLine(System.IO.File.ReadAllLines(Properties.Settings.Default.pathdata + "\\XMLStaticData.xml"));
            HtmlDocument doc = new HtmlDocument();
            int i = 0;
            doc.Load(new StringReader(pagehtml));
            foreach (HtmlNode hn in doc.DocumentNode.SelectNodes("//allmodellist"))
            {
                //StreamWriter sw = new System.IO.StreamWriter(Properties.Settings.Default.pathdata + "\\dflog.txt", true);
                foreach (HtmlNode hnn in hn.SelectNodes("data"))
                {
                    i++;

                    getModelPage(hnn.InnerText, false);
                    //break;
                    //if (i == 5)
                    //    break;
                }

            }
        }
        public void getModelPage(string suri, bool bappend)
        {
            StreamWriter sw = new System.IO.StreamWriter(Properties.Settings.Default.pathdata + "\\definemodellist.txt", bappend);
            //    foreach (string suri in IndexModels)
            //  {
            // Uri myUri = new Uri(IndexModels[0]);
            Uri myUri = new Uri(suri);

            WebClient client = new WebClient();
            string pagehtml = client.DownloadString(myUri);
            //string pagehtml = cCommon.strimLine(System.IO.File.ReadAllLines(Properties.Settings.Default.pathdata + "\\note.htm"));
            //  List<string> lst = new List<string>();




            HtmlDocument doc = new HtmlDocument();

            doc.Load(new StringReader(pagehtml));
            foreach (HtmlNode hn in doc.DocumentNode.SelectNodes("//div[@class='modal_info_full']"))
            {
                DefineBabe db = new DefineBabe();
                try
                {
                    HtmlNode hn2;
                    hn2 = hn.SelectSingleNode("a");
                    if (hn2.Attributes["title"] != null)
                    {
                        db.NAME = hn2.Attributes["title"].Value;
                        //for debug
                        //if (db.NAME.Trim() == "Sandy Joy")
                        //{
                        //}
                        db.URI = "http://www.definebabe.com" + hn2.Attributes["href"].Value;
                        sw.WriteLine(db.URI);
                        hn2 = hn.SelectSingleNode("a/img");
                        db.THUMBNAIL = hn2.Attributes["src"].Value.Replace("thumb1", "thumb2");
                    }

                    if (hn.SelectNodes("div/span") != null)
                        foreach (HtmlNode hnn in hn.SelectNodes("div/span"))
                        {

                            string sinner = hnn.InnerHtml.Trim();
                            if (sinner != string.Empty)
                            {
                                if (sinner.Contains("Birthday:"))
                                {
                                    DateTime tgl;
                                    if (DateTime.TryParse(sinner.Replace("Birthday:", "").Trim(), out tgl))
                                    {
                                        if (tgl.Year > 1950)
                                            db.DOB = sinner.Replace("Birthday:", "").Trim();
                                    }
                                }

                                if (sinner.Contains("Country:"))
                                    db.COUNTRY = sinner.Replace("Country:", "").Trim();

                                if (sinner.Contains("Ethnicity:"))
                                    db.ETNIC = sinner.Replace("Ethnicity:", "").Trim();

                                if (sinner.Contains("Eye:"))
                                    db.EYE = sinner.Replace("Eye:", "").Trim();

                                if (sinner.Contains("Hair:"))
                                    db.HAIR = sinner.Replace("Hair:", "").Trim();

                                if (sinner.Contains("Height:"))
                                    db.HEIGHT = sinner.Replace("Height:", "").Trim();

                                if (sinner.Contains("Weight:"))
                                    db.WEIGHT = sinner.Replace("Weight:", "").Trim();

                                if (sinner.Contains("Cup:"))
                                    db.CUP = sinner.Replace("Cup:", "").Trim();

                                if (sinner.Contains("Measurements:"))
                                    db.MEASURE = sinner.Replace("Measurements:", "").Trim();
                                //NAME	DOB	ALIASES	COUNTRY	STATE	TOWN	ETNIC	EYE	HAIR	WEIGHT	HEIGHT
                                //  CUP	MEASURE	URI	OFFICIALSITE

                            }
                        }

                    string sql;
                    if (db.DOB != null)
                    {
                        sql = "select top 1 modelid from tbmodel where name='" + db.NAME + "' and dob='" + db.DOB + "'";
                    }
                    else
                    {
                        sql = "select top 1 modelid from tbmodel where name='" + db.NAME + "' ";//and dob='" + db.DOB + "'";
                    }
                    if (Dbserver.ExecuteQuery(sql))
                    {
                        db.MODELID = Dbserver.ExecuteScalar(sql).ToString();
                    }
                    else
                    {
                        db.MODELID = new cDBHelper().genIDCommit("DBMODELID");
                    }

                    DataTable dt = new DataTable();
                    dt = getTableStructure("TBMODEL");
                    DataRow dr = dt.NewRow();
                    // dr["RECID"] = 0;// db.MODELID;
                    dr["MODELID"] = db.MODELID;
                    dr["NAME"] = db.NAME;

                    if (db.DOB != null)
                        dr["DOB"] = db.DOB;

                    //   dr["ALIASES"] = db.ALIASES;
                    dr["THUMBNAIL"] = db.THUMBNAIL;
                    dr["COUNTRY"] = db.COUNTRY;
                    dr["STATE"] = db.STATE;
                    dr["TOWN"] = db.TOWN;
                    dr["ETNIC"] = db.ETNIC;
                    dr["EYE"] = db.EYE;
                    dr["HAIR"] = db.HAIR;
                    dr["WEIGHT"] = db.WEIGHT;
                    dr["HEIGHT"] = db.HEIGHT;
                    dr["CUP"] = db.CUP;
                    dr["MEASURE"] = db.MEASURE;
                    dr["URI"] = db.URI;
                    dr["OFFICIALSITE"] = db.OFFICIALSITE;
                    dt.Rows.Add(dr);
                    string serr = "";
                    if (Dbserver.openConn())
                    {
                        Dbserver.SqlUpdate(dt, dt.TableName, out serr);
                        Dbserver.CommitAndCloseConn();
                    }




                }
                catch (Exception ex)
                {


                }


            }


            //    string pagehtml = cCommon.strimLine(System.IO.File.ReadAllLines("D:\\Projek Programming\\2010\\2010\\Extractor\\datanya\\definemodel.htm"));
            //sting

            //
            //}
            sw.Close();

        }
        public void getAllModel()
        {
            StreamWriter sw = new System.IO.StreamWriter(Properties.Settings.Default.pathdata + "\\definemodel.txt", false);
            foreach (string suri in IndexModels)
            {
                // Uri myUri = new Uri(IndexModels[0]);
                Uri myUri = new Uri(suri);
                sw.WriteLine(IndexModels[0]);
                WebClient client = new WebClient();
                string pagehtml = client.DownloadString(myUri);
                //string pagehtml = cCommon.strimLine(System.IO.File.ReadAllLines("D:\\Projek Programming\\2010\\2010\\Extractor\\datanya\\note.htm"));
                List<string> lst = new List<string>();




                HtmlDocument doc = new HtmlDocument();

                doc.Load(new StringReader(pagehtml));
                foreach (HtmlNode hn in doc.DocumentNode.SelectNodes("//div[@class='pages']"))
                {
                    lst.Add(myUri.ToString());
                    try
                    {


                        foreach (HtmlNode hnn in hn.SelectNodes("a"))
                        {

                            if (hnn.Attributes["href"] != null)
                            {
                                if (!hnn.InnerHtml.Contains("Next"))
                                {
                                    lst.Add("http://www.definebabe.com" + hnn.Attributes["href"].Value);
                                    sw.WriteLine("http://www.definebabe.com" + hnn.Attributes["href"].Value);
                                }

                            }
                        }
                    }
                    catch (Exception)
                    {


                    }
                }


                //    string pagehtml = cCommon.strimLine(System.IO.File.ReadAllLines("D:\\Projek Programming\\2010\\2010\\Extractor\\datanya\\definemodel.htm"));
                //sting

                //
            }
            sw.Close();


        }





        
    }

    public class MyThread
    {

        public void DownloadPic()
        {
            StreamWriter sw = new System.IO.StreamWriter(Properties.Settings.Default.pathdata + "\\thread.txt", true);
            sw.WriteLine("");
            sw.Close();
            

        }

    }

}
